package edu.wuster.blog.controller;

import edu.wuster.blog.entity.Blogpart;
import edu.wuster.blog.entity.Result;
import edu.wuster.blog.interf.DefaultGroup;
import edu.wuster.blog.service.BlogpartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/blogpart")
public class BlogpartController {

    @Autowired
    private BlogpartService blogpartService;

    //新增博客-保存/直接发布
    @PostMapping("/addSave")
    public Result addBlogpartSave(@RequestBody Blogpart blogpart) {
        log.info("新增博客请求");
        Integer id= blogpartService.addBlogpartSave(blogpart);
        log.info("新增博客成功");
        return Result.success(id);
    }


    //删除博客
    @DeleteMapping("/{id}")
    public Result deleteBlogpart(@PathVariable Integer id) {
        log.info("博客(id：{})内容删除", id);
        blogpartService.deleteBlogpart(id);
        log.info("博客删除成功");
        return Result.success();
    }

    //根据博客id查看单篇博客
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        return Result.success(blogpartService.selectById(id));
    }

    //更改博客状态
    @PutMapping
    public Result resetById(@RequestBody @Validated(DefaultGroup.class) Blogpart blogpart) {
        blogpartService.resetById(blogpart);
        log.info("博客状态已完成修改");
        return Result.success();
    }

    //新增点赞
    @PutMapping("/like/{id}")
    public Result addLike(@PathVariable Integer id) {
        blogpartService.addLike(id);
        log.info("已新增博客点赞");
        return Result.success();
    }

    //修改博客内容
    @PutMapping("/modify")
    public Result blogModify(@RequestBody Blogpart blogpart ) {
        blogpartService.blogModify(blogpart);
        log.info("已修改博客，待审核");
        return Result.success();
    }


}
